<?php
/**
 * funkcja do obsługi tabelek 
 */
function smarty_function_tables($param, &$smarty) {
	
	if(!isset($param['tid'])) {
		throw new Exception('Musisz podać argument tid do pluginu smarty_function_tables');
	}
	
	$tid = $param['tid'];
	
	$type = isset($param['type'])?$param['type']:null;
	$return = '';
	$table_data = $smarty->get_template_vars('table_data');
	$paramFromUrl = $smarty->get_template_vars('param_from_url');
	
	$not_in_use = array(
		'methodToRun','urlParam'
	);
	
	
	foreach($not_in_use AS $niu)if(isset($paramFromUrl[$niu]))unset($paramFromUrl[$niu]);
	$table_data = $table_data[$tid];
	
	
	switch($type) {
		case 'lp':
			$curr_page = isset($paramFromUrl['strona'])?$paramFromUrl['strona']:1;
			$iteration = isset($param['iteration'])?$param['iteration']:1;
			$total = $table_data['t'];
			
			$return = ($curr_page - 1) * $total + $iteration; 
			
			break;
		case 'on_page':
			
			$partial = isset($param['partial'])?$param['partial']:'partial/Shared/Tables/OnPage.tpl';
			$available = array(
				10, 20, 30, 50
			);
			$onP = array();
			foreach($available AS $count) {
				$onP[] = array(
					'number' => $count,
					'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('tid' => $tid, 't' => $count, 'strona' => 1))),
					'selected' => $count == $table_data['t']?'selected="selected"':''
				);
			}
			
			$smarty->assign('onP',$onP);
			$return = $smarty->fetch($partial);
			break;
		case 'pagination':
			
			$partial = isset($param['partial'])?$param['partial']:'partial/Shared/Tables/Pagination.tpl';
			
			$page_count = ceil($param['all_count']/$table_data['t']);
			$curr_page = isset($paramFromUrl['strona'])?$paramFromUrl['strona']:1;
			
			if($curr_page == 1) {
				$prev_url = Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $curr_page)));
			}else{
				$prev_url = Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $curr_page - 1)));
			}
			if($curr_page + 1 <= $page_count) {
				$next_url = Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $curr_page + 1)));
			}else{
				$next_url = Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $curr_page)));
			}
			$smarty->assign('prevUrl',$prev_url);
			$smarty->assign('nextUrl',$next_url);
			$pages = array();
			$firstpages = array();
			$lastpages = array();
			
			for($i = 1; $i <= $page_count; $i++) {
				if($page_count < 6) {				
					$pages[] = array(
						'number' => $i,
						'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
						'selected' => ($i == $curr_page)?'class="active"':''
					);
				}elseif($page_count < 7 && $curr_page < 4) {
					if($i <= 4){
						$pages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
					if($i == 6){
						$lastpages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
				
				}elseif($page_count < 7 && $curr_page > 3) {
					if($i == 1){
						$firstpages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
					if($i >= 3){
						$pages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
				}elseif($curr_page < 4){
					if($i <= 4){
						$firstpages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
					if($i == $page_count) {
						$pages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);
					}
				}elseif($curr_page > $page_count - 3){
					if($i == 1){
						$pages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
					if($i >= $page_count - 3) {
						$lastpages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);
					}
				}else{
					if($i == 1){
						$firstpages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
					if($i > $curr_page - 3 && $i < $curr_page + 3) {
						$pages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);
					}
					if($i == $page_count){
						$lastpages[] = array(
							'number' => $i,
							'url' => Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('strona' => $i))),
							'selected' => ($i == $curr_page)?'class="active"':''
						);	
					};
				
				}
			}
			
			$smarty->assign('firstpages',$firstpages);
			$smarty->assign('lastpages',$lastpages);
			$smarty->assign('pages',$pages);
			$smarty->assign('page_count', $page_count);
			$return = $smarty->fetch($partial);
			break;
		default:
			// nagłówki tabelki
			
			$field = isset($param['o'])?$param['o']:false;
			$partial = isset($param['partial'])?$param['partial']:'partial/Shared/Tables/Header.tpl';

			
			if($field && $table_data['o'] == $field) {
				if($table_data['v'] == 'malejaco') {
					$descending = 'descendingAct';
					$ascending = 'ascending';	
					$labelUrl = Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('o' => $field,'v' => 'rosnaco', 'tid' => $tid)));
					$active = true;
				}else{
					$descending = 'descending';
					$ascending = 'ascendingAct';
					$labelUrl = Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('o' => $field,'v' => 'malejaco', 'tid' => $tid)));
					$active = true;
				}
			}else{
				$ascending = 'ascending';
				$descending = 'descending';
				$labelUrl = Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('o' => $field,'v' => 'rosnaco', 'tid' => $tid)));
				$active = false;
			}
			
			$smarty->assign('label',isset($param['label'])?$param['label']:'n/a');
			$smarty->assign('noSort',isset($param['noSort'])?$param['noSort']:'');
			$smarty->assign('ascending',$ascending);
			$smarty->assign('ascendingUrl',Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('o' => $field,'v' => 'rosnaco', 'tid' => $tid))));
			$smarty->assign('descending',$descending);
			$smarty->assign('descendingUrl',Router::GenerateUrl('smarty_generated_'.microtime(),array_merge($paramFromUrl, array('o' => $field,'v' => 'malejaco', 'tid' => $tid))));
			$smarty->assign('labelUrl',$labelUrl);
			$smarty->assign('active',$active);
			
			
			$return = $smarty->fetch($partial);
	}
	
	return $return;
}

?>